#!/bin/bash

# Customize Ubuntu based LiveCD
#
# Script by: Nicholas Hughart
# Based on script by: Stephen Clark
# Based on documentation found at:
# https://help.ubuntu.com/community/LiveCDCustomization

#Holds the ISOFILE from the command line.  Read from the second command line arg.  Necessary for extract and build.
iso=$2

#Defines the kernel, currently unused
kernel=2.6.22-14-generic

#Defines the CD/DVD label
label=gOS-live

#Directory squashfs is extracted to
squashdir="edit"

#Directory ISO contents are extracted to
isodir="extract-cd"

function exit_clean
{
  rm /tmp/live.pid
  exit $1
}

#Main Start
clear

if [ -e "/tmp/live.pid" ]; then
echo -n "Someone may be working on the ISO. Do you wish to continue? [y/N]"
read ua
if [ "$ua" = "n" ]; then
	exit 0
fi
fi

echo `date +"%Y-%m-%d %H:%M:%S"` > /tmp/live.pid

date +"Started (%Y-%m-%d %H:%M:%S)"

############################
#   Extract existing ISO   #
############################
if [ "$1" = "extract" ]; then

  echo -n "Loading squashfs module... "
  modprobe squashfs
  echo Done
  echo

  echo -n "Extract iso contents? y/[n] "
  read ua
  if [ "$ua" = "y" ]; then
	if [ -e "$squashdir" ]; then
		echo -n "Removing existing Desktop System... "
		rm -r edit
		echo Done
	fi
	mkdir edit
	if [ -e "$isodir" ]; then
		echo -n "Removing existing CD contents... "
		rm -r extract-cd
		echo Done
	fi
	mkdir $isodir
	if ! [ -e "mnt" ]; then
		mkdir mnt
	fi
	if ! [ -e "squashfs" ]; then
		mkdir squashfs
	fi
	echo -n "Extracting CD contents... "
	mount -o loop $iso mnt
	rsync --exclude=/casper/filesystem.squashfs -a mnt/ extract-cd
	echo Done
	echo -n "Extracting Desktop System... "
	mount -t squashfs -o loop mnt/casper/filesystem.squashfs squashfs
	cp -a squashfs/* edit/
	umount squashfs
	umount mnt
 	rm mnt -r
	rm squashfs -r
	echo Done
  fi
  echo

############################
#      Customize ISO       #
############################
elif [ "$1" = "customize" ]; then

for file in custom.d/*
do 
. $file
done

############################
#        Build ISO         #
############################
elif [ "$1" = "build" ]; then
  rm edit/etc/resolv.conf
  rm edit/etc/hosts

  echo -n "Recompile iso? [y]/n "
  read ua
  if [ "$ua" = "n" ]; then
    exit_clean 0
  fi

  echo "Compressing filesystem"
  if [ -e "extract-cd/casper/filesystem.squashfs" ]; then
    rm extract-cd/casper/filesystem.squashfs
  fi
  mksquashfs edit extract-cd/casper/filesystem.squashfs
  echo

  echo "Removing old md5sum.txt and calculating new md5 sums... "
  rm extract-cd/md5sum.txt
  (cd extract-cd && find . -type f -print0 | xargs -0 md5sum > md5sum.txt)
  echo

  echo "Creating iso"
  if [ -f "$iso" ]; then
    echo -n "Removing old custom iso... "
    rm $iso
    echo Done
    echo
  fi
  cd extract-cd
  mkisofs -r -V "$label" -cache-inodes -J -l -b isolinux/isolinux.bin -c isolinux/boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table -o ../$iso .
  echo

############################
#          Usage           #
############################
else

echo "Usage: customize-livecd.sh [command] [arguments]"

echo "Commands:"
echo "extract"
echo "  Arguments: isofile"
echo 
echo "customize"
echo "  Arguments:"
echo 
echo "build"
echo "  Arguments: isofile"
echo

fi

date +"Finished (%Y-%m-%d %H:%M:%S)"
echo

exit_clean 0
